package com.xb.system.controller;

import com.xb.common.result.Result;
import com.xb.system.service.IOExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author wjh
 * @create 2024-07-22 16:48
 */
@Api(tags = "3、执行判断流程")
@RestController
@RequestMapping("/admin/system/ioExcel")
public class IOExcelController {
    @Resource
    private IOExcel ioExcel;
    
    @Value("${path.output}")
    private String outputFilePath;
    
    @ApiOperation(value = "上传导入模板的Excel文件")
    @PostMapping("/input")
    public Result<String> inputMultipartFile(@RequestParam("file") MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        try {
            ioExcel.inputExcel(file.getInputStream(),originalFilename);
        } catch (Exception e) {
            return Result.fail();
        }
        return Result.ok("判定完成，判定结果文件在" + outputFilePath + originalFilename);
    }

    @ApiOperation(value = "输入导入模板的Excel文件路径")
    @GetMapping("/input")
    public Result<String> inputFileString(String file) throws Exception {
        String originalFilename;
        try(InputStream inputStream = Files.newInputStream(Paths.get(file))) {
            //获取file的文件名，不要前面的目录名
            originalFilename = file.substring(file.lastIndexOf("\\") + 1);
            ioExcel.inputExcel(inputStream,originalFilename);
        } catch (Exception e) {
            return Result.fail();
        }
        return Result.ok("判定完成，判定结果文件在" + outputFilePath + originalFilename);
    }
}
